package com.red.wood.dubbo.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.red.wood.common.util.CommonUtils;
import com.red.wood.dubbo.dao.DataManagerSysDao;
import com.red.wood.dubbo.model.SqlInternetWebSite;
import com.red.wood.dubbo.model.SqlNationalAccRes;
import com.red.wood.dubbo.model.SqlNationalCdnResSurvey;
import com.red.wood.dubbo.model.SqlNationalCloudResSurvey;
import com.red.wood.dubbo.model.SqlNationalDomainRes;
import com.red.wood.dubbo.model.SqlNationalIdcResSurvey;
import com.red.wood.dubbo.model.SqlProvincialAccRes;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

@Repository
public class DataManagerSysDaoImpl implements DataManagerSysDao{

	private static final Log log = LogFactory.getLog(DataManagerSysDaoImpl.class);
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * 全国接入资源视图
	 * 对接企业数量、企业牌照类型 未统计
	 */
	@Override
	public Map<String, List<Map<String, Object>>> getNationalAccResInfo(String day) {
		Map<String, List<Map<String, Object>>> datas = new HashMap<String,List<Map<String,Object>>>();
		try {
			XStream xstream = new XStream(new DomDriver());
			xstream.processAnnotations(SqlNationalAccRes.class);
			String xml = getXmlInfo(day,"sqlNationalAccRes.xml");
			SqlNationalAccRes sql = (SqlNationalAccRes) xstream.fromXML(xml);
			
			//全国接入企业
			List<Map<String,Object>> dataLeft1 = getList(sql.getSqlLeft1());
			datas.put("dataLeft1",dataLeft1);
			
			//全国覆盖资源-->IP地址、机房、顶级域名
			List<Map<String,Object>> dataLeft2 = getList(sql.getSqlLeft2());
			datas.put("dataLeft2",dataLeft2);
			
			//全国接入资源分布-->接入企业数
			List<Map<String,Object>> enterpriseList = getList(sql.getSqlMapEnterprise());
			
			//全国接入资源分布-->机房数
			List<Map<String,Object>> roomList = getList(sql.getSqlMapRoom());
			
			//全国接入资源分布-->IP数
			List<Map<String,Object>> ipList = getList(sql.getSqlMapIp());
			
			//全国接入资源分布-->域名数
			List<Map<String,Object>> domainList = getList(sql.getSqlMapDomain());
			
			//将全国接入资源分布：接入企业数、机房数、IP数、域名数合在一起
			for(Map<String,Object> data : enterpriseList) {
				String name = CommonUtils.valueOf(data.get("name"));
				for(Map<String,Object> room : roomList) {
					String roomName = CommonUtils.valueOf(room.get("name"));
					if(StringUtils.equals(name, roomName)) {
						data.put("room",room.get("room"));
						break;
					}
				}
				for(Map<String,Object> ip : ipList) {
					String ipName = CommonUtils.valueOf(ip.get("name"));
					if(StringUtils.equals(name, ipName)) {
						data.put("ip",ip.get("ip"));
						break;
					}
				}
				for(Map<String,Object> domain : domainList) {
					String domainName = CommonUtils.valueOf(domain.get("name"));
					if(StringUtils.equals(name, domainName)) {
						data.put("domain",domain.get("domain"));
						break;
					}
				}
			}
			datas.put("dataMap", enterpriseList);
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
		return datas;
	}
	/**
	 * 省内接入资源视图
	 * 接入企业数变化趋势、各企业带宽排名 未统计
	 */
	@Override
	public Map<String, List<Map<String, Object>>> getProvincialAccResInfo(String day) {
		Map<String, List<Map<String, Object>>> datas = new HashMap<String,List<Map<String,Object>>>();
		try {
			XStream xstream = new XStream(new DomDriver());
			xstream.processAnnotations(SqlProvincialAccRes.class);
			String xml = getXmlInfo(day,"sqlProvincialAccRes.xml");
			SqlProvincialAccRes sql = (SqlProvincialAccRes) xstream.fromXML(xml);
			
			//接入资源分布
			List<Map<String,Object>> dataMap = getList(sql.getSqlMap());
			datas.put("dataMap",dataMap);
			
			//IDC/ISP业务-->服务区域分类
			List<Map<String,Object>> dataRight1Tab2 = getList(sql.getSqlRight1Tab2());
			datas.put("dataRight1Tab2",dataRight1Tab2);
			
			//云业务-->云业务带宽排行
			List<Map<String,Object>> dataRight2 = getList(sql.getSqlRight2());
			datas.put("dataRight2",dataRight2);
			
			//CDN业务-->CDN企业资源排名
			List<Map<String,Object>> dataRight3 = getList(sql.getSqlRight3());
			datas.put("dataRight3",dataRight3);
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
		return datas;
	}
	
	/**
	 * 重点接入业务视图-->全国IDC/ISP资源概览
	 */
	@Override
	public Map<String, List<Map<String, Object>>> getNationalIdcResSurveyInfo(String day) {
		Map<String, List<Map<String, Object>>> datas = new HashMap<String,List<Map<String,Object>>>();
		try {
			XStream xstream = new XStream(new DomDriver());
			xstream.processAnnotations(SqlNationalIdcResSurvey.class);
			String xml = getXmlInfo(day,"sqlNationalIdcResSurvey.xml");
			SqlNationalIdcResSurvey sql = (SqlNationalIdcResSurvey) xstream.fromXML(xml);
			
			//汇总数据
			List<Map<String,Object>> dataLeft = getList(sql.getSqlLeft());
			datas.put("dataLeft", dataLeft);
			
			//地图数据：企业、机房、域名、IP地址
			List<Map<String,Object>> data = getList(sql.getSqlMap());
			List<Map<String,Object>> dataMap = getIdcResSurveyMap(day,data);
			datas.put("dataMap",dataMap);
			
			//各省接入企业数量排名
			List<Map<String,Object>> dataRight1 = getList(sql.getSqlRight1());
			datas.put("dataRight1",dataRight1);
			
			//增值企业接入域名数量排名
			List<Map<String,Object>> dataRight2 = getList(sql.getSqlRight2());
			datas.put("dataRight2",dataRight2);
			
			//基础企业接入域名数量排名
			List<Map<String,Object>> dataRight3 = getList(sql.getSqlRight3());
			datas.put("dataRight3",dataRight3);
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
		return datas;
	}
	
	/**
	 * 重点接入业务视图-->全国云资源概览
	 */
	@Override
	public Map<String, List<Map<String, Object>>> getNationalCloudResSurveyInfo(String day) {
		Map<String, List<Map<String, Object>>> datas = new HashMap<String,List<Map<String,Object>>>();
		try {
			XStream xstream = new XStream(new DomDriver());
			xstream.processAnnotations(SqlNationalCloudResSurvey.class);
			String xml = getXmlInfo(day,"sqlNationalCloudResSurvey.xml");
			SqlNationalCloudResSurvey sql = (SqlNationalCloudResSurvey) xstream.fromXML(xml);
			
			//汇总数据
			List<Map<String,Object>> dataLeft = getList(sql.getSqlLeft());
			datas.put("dataLeft", dataLeft);
			
			//地图数据：企业、机房、IP地址、域名
			List<Map<String,Object>> data = getList(sql.getSqlMap());
			List<Map<String,Object>> dataMap = getCloudResSurveyMap(day, data);
			datas.put("dataMap",dataMap);
			
			//云企业用户数
			List<Map<String,Object>> dataRightTab1 = getList(sql.getSqlRightTab1());
			datas.put("dataRightTab1", dataRightTab1);
			
			//云企业机房数
			List<Map<String,Object>> dataRightTab2 = getList(sql.getSqlRightTab2());
			datas.put("dataRightTab2", dataRightTab2);
			
			//云企业IP数
			List<Map<String,Object>> dataRightTab3 = getList(sql.getSqlRightTab3());
			datas.put("dataRightTab3", dataRightTab3);
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
		return datas;
	}
	
	/**
	 * 重点接入业务视图-->全国CDN资源概览
	 */
	@Override
	public Map<String, List<Map<String, Object>>> getNationalCdnResSurveyInfo(String day) {
		Map<String, List<Map<String, Object>>> datas = new HashMap<String,List<Map<String,Object>>>();
		try {
			XStream xstream = new XStream(new DomDriver());
			xstream.processAnnotations(SqlNationalCdnResSurvey.class);
			String xml = getXmlInfo(day,"sqlNationalCdnResSurvey.xml");
			SqlNationalCdnResSurvey sql = (SqlNationalCdnResSurvey) xstream.fromXML(xml);
			
			//汇总数据:企业、域名、IP、用户、机房
			List<Map<String,Object>> dataLeft = getList(sql.getSqlLeft());
			datas.put("dataLeft", dataLeft);
			
			//地图数据：企业、域名、IP地址
			List<Map<String,Object>> data = getList(sql.getSqlMap());
			List<Map<String,Object>> dataMap = getCdnResSurveyMap(day, data);
			datas.put("dataMap",dataMap);
			
			//各省CDN IP节点
			List<Map<String,Object>> dataRight1Tab1 = getList(sql.getSqlRight1Tab1());
			datas.put("dataRight1Tab1", dataRight1Tab1);
			
			//各省CDN企业
			List<Map<String,Object>> dataRight1Tab2 = getList(sql.getSqlRight1Tab2());
			datas.put("dataRight1Tab2", dataRight1Tab2);
			
			//各省CDN加速域名
			List<Map<String,Object>> dataRight1Tab3 = getList(sql.getSqlRight1Tab3());
			datas.put("dataRight1Tab3", dataRight1Tab3);
			
			//各企业CDN IP节点
			List<Map<String,Object>> dataRight2Tab1 = getList(sql.getSqlRight2Tab1());
			datas.put("dataRight2Tab1", dataRight2Tab1);
			
			//各企业CDN加速域
			List<Map<String,Object>> dataRight2Tab2 = getList(sql.getSqlRight2Tab2());
			datas.put("dataRight2Tab2", dataRight2Tab2);
			
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
		return datas;
	}
	
	/**
	 * 全国域名资源视图
	 * 省内资源排名TOP5、域名机构排名TOP10 未统计
	 */
	@Override
	public Map<String, List<Map<String, Object>>> getNationalDomainResInfo(String day) {
		Map<String, List<Map<String, Object>>> datas = new HashMap<String,List<Map<String,Object>>>();
		try {
			XStream xstream = new XStream(new DomDriver());
			xstream.processAnnotations(SqlNationalDomainRes.class);
			String xml = getXmlInfo(day,"sqlNationalDomainRes.xml");
			SqlNationalDomainRes sql = (SqlNationalDomainRes) xstream.fromXML(xml);
			
			//标题汇总统计
			List<Map<String,Object>> dataTitleCount = getList(sql.getSqlTitleCount());
			datas.put("dataTitleCount", dataTitleCount);
		
			//资源数量
			List<Map<String,Object>> dataMap = getList(sql.getSqlMap());
			datas.put("dataMap", dataMap);
			
			//国内注册域名规模排名
			List<Map<String,Object>> dataRight = getList(sql.getSqlRight());
			datas.put("dataRight", dataRight);
			
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
		return datas;
	}
	
	/**
	 * 全国互联网网站视图
	 * 企业接入网站数量占比情况 未统计
	 */
	@Override
	public Map<String, List<Map<String, Object>>> getInternetWebSiteInfo(String day) {
		Map<String, List<Map<String, Object>>> datas = new HashMap<String,List<Map<String,Object>>>();
		try {
			XStream xstream = new XStream(new DomDriver());
			xstream.processAnnotations(SqlInternetWebSite.class);
			String xml = getXmlInfo(day,"sqlInternetWebSite.xml");
			SqlInternetWebSite sql = (SqlInternetWebSite) xstream.fromXML(xml);
			
			//全国网站类型占比
			List<Map<String,Object>> dataLeft1 = getList(sql.getSqlLeft1());
			datas.put("dataLeft1", dataLeft1);
			
			//全国网站数量分布、各省网站数量排名
			List<Map<String,Object>> dataMapRight1 = getList(sql.getSqlMapRight1());
			datas.put("dataMapRight1", dataMapRight1);
		
			//网站访问量TOP10
			List<Map<String,Object>> dataRight2 = getList(sql.getSqlRight2());
			datas.put("dataRight2", dataRight2);
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
		return datas;
	}
	
	private List<Map<String, Object>> getIdcResSurveyMap(String day, List<Map<String, Object>> data) {
		List<Map<String, Object>> dataMap = new ArrayList<Map<String,Object>>();
		if(data != null && data.size() > 0) {
			for(Map<String,Object> map : data) {
				Map<String,Object> idcCountMap = new HashMap<String,Object>();
				Map<String,Object> houseCountMap = new HashMap<String,Object>();
				Map<String,Object> domainCountMap = new HashMap<String,Object>();
				Map<String,Object> ipCountMap = new HashMap<String,Object>();
				//企业信息
				idcCountMap.put("province",map.get("province"));
				idcCountMap.put("value", map.get("idc_count"));
				idcCountMap.put("type", "企业");
				idcCountMap.put("day", day);
				dataMap.add(idcCountMap);
				
				//机房信息
				houseCountMap.put("province", map.get("province"));
				houseCountMap.put("value", map.get("house_count"));
				houseCountMap.put("type", "机房");
				houseCountMap.put("day", day);
				dataMap.add(houseCountMap);
				
				//域名信息
				domainCountMap.put("province", map.get("province"));
				domainCountMap.put("value", map.get("domain_count"));
				domainCountMap.put("type", "域名");
				domainCountMap.put("day", day);
				dataMap.add(domainCountMap);
				
				//域名信息
				ipCountMap.put("province", map.get("province"));
				ipCountMap.put("value", map.get("ip_count"));
				ipCountMap.put("type", "IP地址");
				ipCountMap.put("day", day);
				dataMap.add(ipCountMap);
			}
		}
		return dataMap;
	}
	
	private List<Map<String, Object>> getCdnResSurveyMap(String day, List<Map<String, Object>> data) {
		List<Map<String, Object>> dataMap = new ArrayList<Map<String,Object>>();
		if(data != null && data.size() > 0) {
			for(Map<String,Object> map : data) {
				Map<String,Object> cdnCountMap = new HashMap<String,Object>();
				Map<String,Object> domainCountMap = new HashMap<String,Object>();
				Map<String,Object> ipCountMap = new HashMap<String,Object>();
				//企业信息
				cdnCountMap.put("province",map.get("province"));
				cdnCountMap.put("value", map.get("cdn_count"));
				cdnCountMap.put("type", "企业");
				cdnCountMap.put("day", day);
				dataMap.add(cdnCountMap);
				
				//域名信息
				domainCountMap.put("province", map.get("province"));
				domainCountMap.put("value", map.get("domain_count"));
				domainCountMap.put("type", "域名");
				domainCountMap.put("day", day);
				dataMap.add(domainCountMap);
				
				//域名信息
				ipCountMap.put("province", map.get("province"));
				ipCountMap.put("value", map.get("ip_count"));
				ipCountMap.put("type", "IP地址");
				ipCountMap.put("day", day);
				dataMap.add(ipCountMap);
			}
		}
		return dataMap;
	}
	
	private List<Map<String, Object>> getCloudResSurveyMap(String day, List<Map<String, Object>> data) {
		List<Map<String, Object>> dataMap = new ArrayList<Map<String,Object>>();
		if(data != null && data.size() > 0) {
			for(Map<String,Object> map : data) {
				Map<String,Object> unitCountMap = new HashMap<String,Object>();
				Map<String,Object> houseCountMap = new HashMap<String,Object>();
				Map<String,Object> ipCountMap = new HashMap<String,Object>();
				Map<String,Object> domainCountMap = new HashMap<String,Object>();
				
				//企业信息
				unitCountMap.put("province",map.get("province"));
				unitCountMap.put("value", map.get("unit_count"));
				unitCountMap.put("type", "企业");
				unitCountMap.put("day", day);
				dataMap.add(unitCountMap);
				
				//机房信息
				houseCountMap.put("province", map.get("province"));
				houseCountMap.put("value", map.get("house_count"));
				houseCountMap.put("type", "机房");
				houseCountMap.put("day", day);
				dataMap.add(houseCountMap);
				
				//IP地址信息
				ipCountMap.put("province", map.get("province"));
				ipCountMap.put("value", map.get("ip_count"));
				ipCountMap.put("type", "IP地址");
				ipCountMap.put("day", day);
				dataMap.add(ipCountMap);
				
				//域名信息
				domainCountMap.put("province", map.get("province"));
				domainCountMap.put("value", map.get("domain_count"));
				domainCountMap.put("type", "域名");
				domainCountMap.put("day", day);
				dataMap.add(domainCountMap);
			}
		}
		return dataMap;
	}
	
	/**
	 * 获取配置文件中的信息
	 * @param day
	 * @param config
	 * @return
	 */
	private String getXmlInfo(String day,String config) {
		String xml = "";
		try {
			ClassLoader classLoader = DataManagerSysDaoImpl.class.getClassLoader();  
			xml = IOUtils.toString(classLoader.getResourceAsStream("sqlconfig/"+config),"UTF-8");  
			//特殊处理，用于传入参数
			xml = xml.replace("$day", day);
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
		return xml;
	}
	
	public List<Map<String,Object>> getList(String sql){
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = jdbcTemplate.queryForList(sql);
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
		return list;
	}
}
